﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using BuXiaChu.OrderDishes.BLL.WebRuntime;
using ModulesNS = BuXiaChu.OrderDishes.Web.CallCenter.Modules;
using BuXiaChu.OrderDishes.BLL;
using BuXiaChu.OrderDishes.Model;

namespace BuXiaChu.OrderDishes.Web.CallCenter.Order
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class GetDataList : HttpHandlerBase, IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            base.Init(context);

            var t = GetFormValue("t");
            switch (t)
            {
                case "1":
                    GetRestaurantList();
                    break;
                case "2":
                    GetDishesList();
                    break;
                case "3":
                    GetOrderList();
                    break;
                case "4":
                    GetNoticeList();
                    break;
                default:
                    AjaxActionResult result = new AjaxActionResult();
                    result.message = "未知的操作 t:" + t;
                    SerializeDataToResponseStream(result);
                    break;
            }
        }

        private void GetRestaurantList()
        {
            #region
            var areaId = GetFormValue("areaId");

            AjaxActionResult result = new AjaxActionResult();
            try
            {
                UserControlPreviewManager<ModulesNS.RestaurantList> preivewManager = new UserControlPreviewManager<ModulesNS.RestaurantList>();
                ModulesNS.RestaurantList dataListControl = preivewManager.LoadUserControl("~/Modules/RestaurantList.ascx");
                dataListControl.AreaId = int.Parse(areaId);
                dataListControl.RestaurantName = GetFormValue("RestaurantName");
                dataListControl.DishesName = GetFormValue("DishesName");
                result.message = preivewManager.RenderHtml(dataListControl);
                result.success = true;
            }
            catch (Exception ex)
            {
                ExceptionMessageManager.Record(ex);
            }

            SerializeDataToResponseStream(result);
            #endregion
        }

        private void GetDishesList()
        {
            #region
            var restaurantId = GetFormValue("id");

            AjaxActionResult result = new AjaxActionResult();
            try
            {
                UserControlPreviewManager<ModulesNS.DishesList> preivewManager = new UserControlPreviewManager<ModulesNS.DishesList>();
                ModulesNS.DishesList dataListControl = preivewManager.LoadUserControl("~/Modules/DishesList.ascx");
                dataListControl.RestaurantId = int.Parse(restaurantId);
                dataListControl.RestaurantName = GetFormValue("RestaurantName");
                result.message = preivewManager.RenderHtml(dataListControl);
                result.success = true;
            }
            catch (Exception ex)
            {
                ExceptionMessageManager.Record(ex);
            }

            SerializeDataToResponseStream(result);
            #endregion
        }

        private void GetOrderList()
        {
            #region
            AjaxActionResult result = new AjaxActionResult();
            try
            {
                UserControlPreviewManager<ModulesNS.OrderList> preivewManager = new UserControlPreviewManager<ModulesNS.OrderList>();
                ModulesNS.OrderList dataListControl = preivewManager.LoadUserControl("~/Modules/OrderList.ascx");
                dataListControl.OrderId = GetFormValue("orderId");
                dataListControl.CallerId = GetFormValue("callerId");
                dataListControl.Address = GetFormValue("address");
                result.message = preivewManager.RenderHtml(dataListControl);
                result.success = true;
            }
            catch (Exception ex)
            {
                ExceptionMessageManager.Record(ex);
            }

            SerializeDataToResponseStream(result);
            #endregion
        }

        private void GetNoticeList()
        {
            #region
            AjaxActionResult result = new AjaxActionResult();
            try
            {
                var id = GetFormValue("id");
                var sqlWhere = " Id>" + (string.IsNullOrEmpty(id) ? "0" : id);
                var pageCount = 0;
                List<NoticeBoardModel> list = NoticeBoardManager.Search(sqlWhere, 0, 0, out pageCount);
                var tempList = new List<Object>();
                list.ForEach(item =>
                {
                    tempList.Add(new
                    {
                        id=item.Id,
                        publisher = item.Publisher,
                        createDate = item.CreateDate.ToString("yyyy-MM-dd HH:MM:s"),
                        content = item.Content,
                        levelName = CodeDictionary.Instance.GetCodeName(item.Level),
                        level = item.Level
                    });
                });
                result.obj = tempList;
                result.success = true;
            }
            catch (Exception ex)
            {
                ExceptionMessageManager.Record(ex);
            }

            SerializeDataToResponseStream(result);
            #endregion
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

}
